//
//  ViewController.m
//  10-UI
//
//  Created by Snow on 16/7/27.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
#import "YiCheTableViewCell.h"
#import "Album.h"
#import "UIImageView+WebCache.h"
#define kReuse @"reuse"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViewOfYiChe;
@property (nonatomic, strong) NSMutableArray *arrayOfYiChe;
@property (nonatomic, strong) NSMutableArray *arrOfImages;
@property (nonatomic, strong) NSMutableArray *arrOfImagesHeader;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *1.根据提供的Plist数据完成简易版易车界面（无图片显示)
     注意: plist文件读取出来是个NSDictionary, 打印之后根据内容解析转换成相应的类
     *
     *  @return
     */
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YiChe" ofType:@"plist"];
    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"%@", dictionary);
    self.arrayOfYiChe = [NSMutableArray array];
    self.arrOfImages = [NSMutableArray array];
    self.arrOfImagesHeader = [NSMutableArray array];
    NSDictionary *dic = [dictionary objectForKey:@"data"];
    NSArray *array = [dic objectForKey:@"list"];
    for (NSDictionary *dicOfYiChe in array) {
        [self.arrayOfYiChe addObject:[dicOfYiChe objectForKey:@"title"]];
        [self.arrOfImages addObject:[dicOfYiChe objectForKey:@"picCover"]];
    }
    NSArray *arrayOfHeader = [dic objectForKey:@"cycleImage"];
    for (NSDictionary *dicOfHeader in arrayOfHeader) {
        [self.arrOfImagesHeader addObject:[dicOfHeader objectForKey:@"picCover"]];
    }
    self.tableViewOfYiChe = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableViewOfYiChe.delegate = self;
    self.tableViewOfYiChe.dataSource = self;
    [self.view addSubview:self.tableViewOfYiChe];
    [self.tableViewOfYiChe registerClass:[YiCheTableViewCell class] forCellReuseIdentifier:kReuse];
    UIView *viewOfHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 280)];

    Album *album = [[Album alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 180) images:_arrOfImagesHeader];
    [viewOfHeader addSubview:album];
    NSArray *arrOfTitle = @[@"贷款买车", @"直销车型", @"底价买车", @"二手车"];
    NSArray *arrOfPic = @[@"1", @"2", @"3", @"4"];
    for (int i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.view.frame.size.width / 4 * i + i, 180, self.view.frame.size.width / 4, 80);
        [button setImage:[UIImage imageNamed:arrOfPic[i]] forState:UIControlStateNormal];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 4 * i + i, 260, self.view.frame.size.width / 4, 20)];
        label.text = arrOfTitle[i];
        label.backgroundColor = [UIColor whiteColor];
        [viewOfHeader addSubview:label];
        label.textAlignment = 1;
        [viewOfHeader addSubview:button];
    }
    
    self.tableViewOfYiChe.tableHeaderView = viewOfHeader;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrayOfYiChe.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YiCheTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kReuse];
    cell.labelOfContent.text = [_arrayOfYiChe objectAtIndex:indexPath.row];
    NSString *string = [_arrOfImages objectAtIndex:indexPath.row];
    NSURL *url = [NSURL URLWithString:string];
    [cell.imageViewOfYiChe sd_setImageWithURL:url];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
